﻿using System;

namespace core
{
    /// <summary>Trieda IMultiRenameData_Str</summary>
    public class IMultiRenameData_Str
    {
        public System.Collections.Generic.List<string> SourceFileNames;
        public System.Collections.Generic.List<string> DestinationFileNames;

        /// <summary>Konstruktor</summary>
        public IMultiRenameData_Str()
        {
            SourceFileNames = new System.Collections.Generic.List<string>();
            DestinationFileNames = new System.Collections.Generic.List<string>();
        }
        /// <summary>Skopirovanie obsahu objektu</summary>
        /// <param name="pValue">objekt pre skopirovanie</param>
        public void Assign(IMultiRenameData_Str pValue)
        {
            SourceFileNames.Clear(); SourceFileNames.AddRange(pValue.SourceFileNames);
            DestinationFileNames.Clear(); DestinationFileNames.AddRange(pValue.DestinationFileNames);
        }

        /// <summary>Editacia pomocou formulara</summary>
        /// <returns>true / false</returns>
        public bool EditWithForm()
        {
            using (IFileExplorerForm_MultiRenameTool F = new IFileExplorerForm_MultiRenameTool())
            {
                F.Value.Assign(this);
                bool B = F.ShowDialog() == System.Windows.Forms.DialogResult.OK;
                if (B) Assign(F.Value);
                return B;
            }
        }
    }

    /// <summary>Trieda IFileExplorerForm_MultiRenameTool</summary>
    public class IFileExplorerForm_MultiRenameTool : IForm
    {
        /// <summary>Trieda IRenameOperation</summary>
        protected class IRenameOperation
        {
            public const int APPLY_TO_FULL_NAME = 0;
            public const int APPLY_TO_NAME_ONLY = 1;
            public const int APPLY_TO_EXTENSION = 2;

            public const int ID_UPPER_CASE = 1;
            public const int ID_LOWER_CASE = 2;
            public const int ID_FIRST_UPPER_CASE = 3;
            public const int ID_FIRST_WORD_UPPER_CASE = 4;
            public const int ID_MASKED_RENAME = 5;
            public const int ID_REPLACE_SUBSTRING = 6;

            public int ID; // ID objekty
            protected int FApplyTo; // Sposob aplikacie

            /// <summary>Rozdelenie nazvu</summary>
            /// <param name="pValue">vstup</param>
            /// <param name="ResultName">vysledny nazov bez pripony</param>
            /// <param name="ResultExt">vysledna pripona</param>
            protected void SplitName(string pValue, out string ResultName, out string ResultExt)
            {
                int I = pValue.LastIndexOf('.');
                if (I == -1)
                {
                    ResultName = pValue;
                    ResultExt = "";
                }
                else
                {
                    ResultName = pValue.Substring(0, I);
                    ResultExt = pValue.Substring(I + 1);
                }
            }
            /// <summary>Spojenie nazvu a pripony</summary>
            /// <param name="pNameOnly">nazov</param>
            /// <param name="pExtension">pripona</param>
            /// <returns>vysledok</returns>
            protected string JoinName(string pNameOnly, string pExtension)
            {
                return pExtension == "" ? pNameOnly : pNameOnly + "." + pExtension;
            }

            /// <summary>Property: Name</summary>
            public virtual string Name
            {
                get
                {
                    return "";
                }
            }
            /// <summary>Property: ApplyTo</summary>
            public int ApplyTo
            {
                get
                {
                    return FApplyTo;
                }
                set
                {
                    FApplyTo = value;
                }
            }
            /// <summary>Property: Counter</summary>
            public virtual long Counter
            {
                get
                {
                    return 0;
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation(int pApplyTo = APPLY_TO_FULL_NAME)
            {
                FApplyTo = pApplyTo;
                ID = 0;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public virtual string DoRename(string pValue, long pCounter = 1)
            {
                return pValue;
            }
            /// <summary>Inicializacia pocitadla</summary>
            public virtual void InitCounter()
            {
            }
            /// <summary>Zvysenie pocitadla</summary>
            public virtual void IncreaseCounter()
            {
            }
        }

        /// <summary>Trieda IRenameOperation_UpperCase</summary>
        protected class IRenameOperation_UpperCase : IRenameOperation
        {
            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_1, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_1, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_1, "");
                    }
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_UpperCase(int pApplyTo = APPLY_TO_FULL_NAME) : base(pApplyTo) 
            {
                ID = ID_UPPER_CASE;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                switch (FApplyTo)
                {
                    case APPLY_TO_NAME_ONLY: N = N.ToUpper(); break;
                    case APPLY_TO_EXTENSION: E = E.ToUpper(); break;
                    default:
                        {
                            N = N.ToUpper(); E = E.ToUpper();
                        } break;
                }

                return JoinName(N, E); // Spojim a vratim vysledok
            }
        }

        /// <summary>Trieda IRenameOperation_LowerCase</summary>
        protected class IRenameOperation_LowerCase : IRenameOperation
        {
            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_4, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_4, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_4, "");
                    }
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_LowerCase(int pApplyTo = APPLY_TO_FULL_NAME)
                : base(pApplyTo)
            {
                ID = ID_LOWER_CASE;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                switch (FApplyTo)
                {
                    case APPLY_TO_NAME_ONLY: N = N.ToLower(); break;
                    case APPLY_TO_EXTENSION: E = E.ToLower(); break;
                    default:
                        {
                            N = N.ToLower(); E = E.ToLower();
                        } break;
                }

                return JoinName(N, E); // Spojim a vratim vysledok
            }
        }

        /// <summary>Trieda IRenameOperation_FirstUpperCase</summary>
        protected class IRenameOperation_FirstUpperCase : IRenameOperation
        {
            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_5, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_5, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_5, "");
                    }
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_FirstUpperCase(int pApplyTo = APPLY_TO_FULL_NAME)
                : base(pApplyTo)
            {
                ID = ID_FIRST_UPPER_CASE;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                switch (FApplyTo)
                {
                    case APPLY_TO_EXTENSION: E = ICore.FirstUpperChar(E); break;
                    default:
                        {
                            N = ICore.FirstUpperChar(N);
                        } break;
                }

                return JoinName(N, E); // Spojim a vratim vysledok
            }
        }

        ///// <summary>Trieda IRenameOperation_FirstWordUpperCase</summary>
        protected class IRenameOperation_FirstWordUpperCase : IRenameOperation
        {
            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_6, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_6, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_6, "");
                    }
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_FirstWordUpperCase(int pApplyTo = APPLY_TO_FULL_NAME)
                : base(pApplyTo)
            {
                ID = ID_FIRST_WORD_UPPER_CASE;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                switch (FApplyTo)
                {
                    case APPLY_TO_EXTENSION: E = ICore.FirstWordUpperChar(E); break;
                    case APPLY_TO_NAME_ONLY: N = ICore.FirstWordUpperChar(N); break;
                    default:
                        {
                            N = ICore.FirstWordUpperChar(N);
                            E = ICore.FirstWordUpperChar(E);
                        } break;
                }

                return JoinName(N, E); // Spojim a vratim vysledok
            }
        }

        /// <summary>Trieda IRenameOperation_MaskedRename</summary>
        protected class IRenameOperation_MaskedRename : IRenameOperation
        {
            /// <summary>Trieda IForm_MaskedRenameEditor</summary>
            protected class IForm_MaskedRenameEditor : IForm
            {
                public IRenameOperation_MaskedRename Value;
                public string ExampleFileName;

                protected IGroupBox FGrp;
                protected IButton FBtn_Cancel;
                protected IButton FBtn_OK;

                protected ILabel FLabel1;
                protected ITextBox FText_MaskName;
                protected ILabel FLabel2;
                protected ITextBox FText_MaskExt;
                protected ILabel FLabel3, FLabel4, FLabel5;
                protected ITextBox FText_CounterStart;
                protected ITextBox FText_CounterIncrement;
                protected IComboBox FCombo_CounterNumbers;
                protected IGroupBox FGrp1;
                protected ILabel FLabel6;
                protected ITextBox FText_Example;
                protected IButton FBtn_Choose_Name;
                protected IButton FBtn_Choose_Ext;

                /// <summary>Konstruktor</summary>
                public IForm_MaskedRenameEditor()
                {
                    Value = new IRenameOperation_MaskedRename();
                }

                /// <summary>Metoda: OnCreateForm</summary>
                /// <returns>true / false</returns>
                protected override bool OnCreateForm()
                {
                    if (!base.OnCreateForm()) return false; // Volam predchodcu

                    // Inicializacia
                    MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = true; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

                    SetBounds(0, 0, 400, 340); System.Drawing.Size CS = ClientSize;
                    Text = lang.FORM_MULTI_RENAME_TOOL_TEXT_10;

                    // Vytvorim groupbox
                    FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);
                    // Vytvorim tlacidla
                    FBtn_Cancel = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_2); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                    FBtn_OK = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_9, Event_BtnClickOK); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);
                    Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

                    FLabel1 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_1);
                    FText_MaskName = new ITextBox(); 
                    FLabel2 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_2);
                    FText_MaskExt = new ITextBox();
                    FGrp1 = new IGroupBox(); FGrp1.Text = lang.FORM_MULTI_RENAME_TOOL_TEXT_10_8;
                    FLabel3 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_4);
                    FLabel4 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_5);
                    FLabel5 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_6);
                    FText_CounterStart = new ITextBox(); FText_CounterIncrement = new ITextBox();
                    FCombo_CounterNumbers = new IComboBox(); FCombo_CounterNumbers.AddMultiItems(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_7, '\b', new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
                    FLabel6 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_9);
                    FText_Example = new ITextBox(); FText_Example.ReadOnly = true;
                    FBtn_Choose_Name = new IButton("..", Event_BtnClick_AddName, false); FBtn_Choose_Ext = new IButton("..", Event_BtnClick_AddExt, false);

                    FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel1, FText_MaskName, FBtn_Choose_Name, FLabel2, FText_MaskExt, FBtn_Choose_Ext, FGrp1, FLabel6, FText_Example });
                    FGrp1.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel3, FText_CounterStart, FLabel4, FText_CounterIncrement, FLabel5, FCombo_CounterNumbers });
                    IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel1, FGrp, 10, 18);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_MaskName, FLabel1, 0, 6, FGrp.ClientSize.Width - 60);
                    IControl.PlaceControl_Outside_LeftTop_TopRight(FBtn_Choose_Name, FText_MaskName, 0, 10, 30, FText_MaskName.Height);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel2, FText_MaskName, 0, 10);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_MaskExt, FLabel2, 0, 6, FGrp.ClientSize.Width - 60);
                    IControl.PlaceControl_Outside_LeftTop_TopRight(FBtn_Choose_Ext, FText_MaskExt, 0, 10, 30, FText_MaskExt.Height);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FGrp1, FText_MaskExt, 0, 10, FGrp.ClientSize.Width - 20, 100);

                    IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel3, FGrp1, 10, 22);
                    IControl.PlaceControl_Outside_LeftTop_LeftTop(FLabel4, FLabel3, 112, 0);
                    IControl.PlaceControl_Outside_LeftTop_LeftTop(FLabel5, FLabel4, 112, 0);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_CounterStart, FLabel3, 0, 6, 100);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_CounterIncrement, FLabel4, 0, 6, 100);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FCombo_CounterNumbers, FLabel5, 0, 6, 100);
                    FGrp1.Height = FCombo_CounterNumbers.Bottom + 10;

                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel6, FGrp1, 0, 8);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_Example, FLabel6, 0, 6, FGrp.ClientSize.Width - 20);

                    CancelButton = FBtn_Cancel; AcceptButton = FBtn_OK;

                    FillForm(Value); UpdateExampleValue(Value);

                    FText_MaskName.TextChanged += Event_Changed;
                    FText_MaskExt.TextChanged += Event_Changed;
                    FText_CounterStart.TextChanged += Event_Changed;
                    FText_CounterIncrement.TextChanged += Event_Changed;
                    FCombo_CounterNumbers.SelectedIndexChanged += Event_Changed;
                    return true; // Vsetko OK
                }

                /// <summary>Pregenerovanie prikladu</summary>
                protected void UpdateExampleValue(IRenameOperation_MaskedRename pValue)
                {
                    FText_Example.Text = pValue.DoRename(ExampleFileName, pValue.Counter_Start);
                }
                /// <summary>Vyplnenie formulara</summary>
                /// <param name="pValue">objekt</param>
                protected void FillForm(IRenameOperation_MaskedRename pValue)
                {
                    FText_MaskName.Text = pValue.Mask_Name;
                    FText_MaskExt.Text = pValue.Mask_Ext;
                    FText_CounterStart.Text = pValue.Counter_Start.ToString();
                    FText_CounterIncrement.Text = pValue.Counter_Increment.ToString();
                    FCombo_CounterNumbers.SelectedIntID = pValue.Counter_Numbers;
                }
                /// <summary>Nacitanie obsahu</summary>
                /// <param name="Result">vysledok</param>
                protected void ReadForm(IRenameOperation_MaskedRename Result)
                {
                    Result.Mask_Name = FText_MaskName.Text;
                    Result.Mask_Ext = FText_MaskExt.Text;
                    Result.Counter_Start = ICore.ToInt(FText_CounterStart.Text);
                    if (Result.Counter_Start <= 0) Result.Counter_Start = 1;
                    Result.Counter_Increment = ICore.ToInt(FText_CounterIncrement.Text);
                    if (Result.Counter_Increment <= 0) Result.Counter_Increment = 1;
                    Result.Counter_Numbers = FCombo_CounterNumbers.SelectedIntID;
                }
                /// <summary>Vytvorenie menu</summary>
                /// <param name="pParent">rodicovska kolekcia</param>
                /// <param name="pDestination">cielovy textbox</param>
                protected void CreatePopupMenu(System.Windows.Forms.ToolStripItemCollection pParent, ITextBox pDestination)
                {
                    pParent.Clear();
                    IToolStripMenuItem M1, M2;

                    M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_1);
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_1_1, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[N]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_1_2, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[N1]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_1_3, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[N1:1]";

                    IToolStrip.CreateSeparator(pParent);
                    M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_2);
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_2_1, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[E]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_2_2, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[E1]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_2_3, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[E1:1]";

                    IToolStrip.CreateSeparator(pParent);
                    M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_3, Event_MIClick); M1.Tag = pDestination; M1.UserParameter = "[C]";

                    IToolStrip.CreateSeparator(pParent);
                    M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4);
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_1, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[Y]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_2, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[YYYY]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_3, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[M]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_4, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[MM]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_5, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[MMM]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_6, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[MMMM]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_7, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[D]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_8, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[DD]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_9, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[DDD]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_10, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[DDDD]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_11, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[H]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_12, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[HH]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_13, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[n]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_14, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[nn]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_15, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[S]";
                    M2 = M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_10_10_4_16, Event_MIClick); M2.Tag = pDestination; M2.UserParameter = "[SS]";
                }

                /// <summary>Event: Changed</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_Changed(object pSender, EventArgs e)
                {
                    IRenameOperation_MaskedRename V = new IRenameOperation_MaskedRename();
                    ReadForm(V);
                    UpdateExampleValue(V);
                }
                /// <summary>Event: Click pre tlacidlo OK</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_BtnClickOK(object pSender, EventArgs e)
                {
                    ReadForm(Value);
                    UpdateExampleValue(Value);
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                /// <summary>Event: OnClick pre polozku menu</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_MIClick(object pSender, EventArgs e)
                {
                    IToolStripMenuItem M = (IToolStripMenuItem)pSender;
                    ((ITextBox)M.Tag).Text += M.UserParameter;
                }
                /// <summary>Event: Click pre tlacidlo 'Pridat ku nazvu'</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_BtnClick_AddName(object pSender, EventArgs e)
                {
                    System.Windows.Forms.ContextMenuStrip M = new System.Windows.Forms.ContextMenuStrip();
                    CreatePopupMenu(M.Items, FText_MaskName);
                    M.Show(MousePosition);
                }
                /// <summary>Event: Click pre tlacidlo 'Pridat ku pripone'</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_BtnClick_AddExt(object pSender, EventArgs e)
                {
                    System.Windows.Forms.ContextMenuStrip M = new System.Windows.Forms.ContextMenuStrip();
                    CreatePopupMenu(M.Items, FText_MaskExt);
                    M.Show(MousePosition);
                }
            }

            public string Mask_Name;
            public string Mask_Ext;
            public long Counter_Start;
            public long Counter_Increment;
            public int Counter_Numbers;
            public long Counter_Value;

            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_7, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_7, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_7, "");
                    }
                }
            }
            /// <summary>Property: Counter</summary>
            public override long Counter
            {
                get
                {
                    return Counter_Value;
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_MaskedRename(int pApplyTo = APPLY_TO_FULL_NAME)
                : base(pApplyTo)
            {
                ID = ID_MASKED_RENAME;
                Counter_Value = Counter_Start = 1; Counter_Increment = 1; Counter_Numbers = 1;
                Mask_Name = "[N]"; Mask_Ext = "[E]";
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                string N1 = DoRename1(Mask_Name, N, E, pCounter);
                string E1 = DoRename1(Mask_Ext, N, E, pCounter);

                return E1 == "" ? N1 : N1 + "." + E1; // Poskladam vysledok
            }
            /// <summary>Inicializacia pocitadla</summary>
            public override void InitCounter()
            {
                Counter_Value = Counter_Start;
            }
            /// <summary>Zvysenie pocitadla</summary>
            public override void IncreaseCounter()
            {
                Counter_Value += Counter_Increment;
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pMask">maska</param>
            /// <param name="pName">nazov</param>
            /// <param name="pExt">pripona</param>
            /// <param name="pCounter">pocitadlo</param>
            /// <returns>vysledok</returns>
            protected string DoRename1(string pMask, string pName, string pExt, long pCounter)
            {
                System.Text.StringBuilder B = new System.Text.StringBuilder();
                
                DateTime Now = DateTime.Now;
                int C = pMask.Length;
                int Mode = 0;
                int I1 = 0, I2 = 0, L = 0;
                for (int i = 0; i < C; i++) 
                {
                    char CH = pMask[i];
                    switch (Mode)
                    {
                        case 1:
                            {
                                if (CH == '[')
                                {
                                    B.Append(pMask.Substring(I1, i - I1));
                                    I1 = i;
                                    continue;
                                }
                                if (CH != ']') continue;
                                I2 = i;

                                I1++; L = I2 - I1;
                                if (L == 0) 
                                {
                                    Mode = 0;
                                    continue;
                                }
                                string S = pMask.Substring(I1, L);
                                if (string.Compare(S, "y", true) == 0)
                                {
                                    B.Append(Now.ToString("yy"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "yyyy", true) == 0)
                                {
                                    B.Append(Now.ToString("yyyy"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "m", true) == 0)
                                {
                                    B.Append(Now.Month.ToString());
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "mm", true) == 0)
                                {
                                    B.Append(Now.Month.ToString("D2"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "mmm", true) == 0)
                                {
                                    B.Append(Now.ToString("MMM"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "mmmm", true) == 0)
                                {
                                    B.Append(Now.ToString("MMMM"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "d", true) == 0)
                                {
                                    B.Append(Now.ToString("d"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "dd", true) == 0)
                                {
                                    B.Append(Now.ToString("dd"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "ddd", true) == 0)
                                {
                                    B.Append(Now.ToString("ddd"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "dddd", true) == 0)
                                {
                                    B.Append(Now.ToString("dddd"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "h", true) == 0)
                                {
                                    B.Append(Now.ToString("H"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "hh", true) == 0)
                                {
                                    B.Append(Now.ToString("HH"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "n", false) == 0)
                                {
                                    B.Append(Now.ToString("m"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "nn", false) == 0)
                                {
                                    B.Append(Now.ToString("mm"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "s", true) == 0)
                                {
                                    B.Append(Now.ToString("s"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "ss", true) == 0)
                                {
                                    B.Append(Now.ToString("ss"));
                                    Mode = 0;
                                    continue;
                                }
                                if (string.Compare(S, "c", true) == 0)
                                {
                                    B.Append(pCounter.ToString(string.Format("D{0}", Counter_Numbers)));
                                    Mode = 0;
                                    continue;
                                }
                                if (S.StartsWith("N"))
                                {
                                    if (string.Compare(S, "N", false) == 0)
                                    {
                                        B.Append(pName);
                                        Mode = 0;
                                        continue;
                                    }
                                    int I = S.IndexOf(':');
                                    int Start = 0;
                                    int Length = 0;

                                    if (I == -1)
                                    {
                                        Start = ICore.ToInt(S.Substring(1));
                                        Length = pName.Length;
                                    }
                                    else
                                    {
                                        Start = ICore.ToInt(S.Substring(1, I - 1));
                                        Length = ICore.ToInt(S.Substring(I + 1));
                                    }

                                    int L1 = pName.Length - Start + 1;
                                    if ((Start > 0) && (Length > 0))
                                    {
                                        if (Length > L1) Length = L1;
                                        B.Append(pName.Substring(Start - 1, Length));
                                        Mode = 0;
                                        continue;
                                    }
                                }
                                if (S.StartsWith("E"))
                                {
                                    if (string.Compare(S, "E", false) == 0)
                                    {
                                        B.Append(pExt);
                                        Mode = 0;
                                        continue;
                                    }

                                    int I = S.IndexOf(':');
                                    int Start = 0;
                                    int Length = 0;
                                    if (I == -1)
                                    {
                                        Start = ICore.ToInt(S.Substring(1));
                                        Length = pExt.Length;
                                    }
                                    else
                                    {
                                        Start = ICore.ToInt(S.Substring(1, I - 1));
                                        Length = ICore.ToInt(S.Substring(I + 1));
                                    }

                                    int L1 = pExt.Length - Start + 1;
                                    if ((Start > 0) && (Length > 0))
                                    {
                                        if (Length > L1) Length = L1;
                                        B.Append(pExt.Substring(Start - 1, L1));
                                        Mode = 0;
                                        continue;
                                    }
                                }

                                B.Append(pMask.Substring(I1, i - I1));

                                Mode = 0;
                                continue;
                            }
                        default:
                            {
                                if (CH != '[')
                                {
                                    B.Append(CH);
                                    continue;
                                }
                                
                                Mode = 1;
                                I1 = i;
                                continue;
                            }
                    }
                }

                return B.ToString();
            }

            /// <summary>Skopirovanie obsahu objektu</summary>
            /// <param name="pValue">objekt pre skopirovanie</param>
            public void Assign(IRenameOperation_MaskedRename pValue)
            {
                FApplyTo = pValue.FApplyTo;
                Counter_Increment = pValue.Counter_Increment;
                Counter_Numbers = pValue.Counter_Numbers;
                Counter_Start = pValue.Counter_Start;
                Mask_Name = pValue.Mask_Name;
                Mask_Ext = pValue.Mask_Ext;
            }
            /// <summary>Zmena parametrov pomocou formulara</summary>
            /// <returns></returns>
            public bool EditWithForm(string pTestValue)
            {
                using (IForm_MaskedRenameEditor F = new IForm_MaskedRenameEditor())
                {
                    F.Value.Assign(this); F.ExampleFileName = pTestValue;
                    bool B = F.ShowDialog() == System.Windows.Forms.DialogResult.OK;
                    if (B) Assign(F.Value);
                    return B;
                }
            }
        }

        ///// <summary>Trieda IRenameOperation_ReplaceSubstring</summary>
        protected class IRenameOperation_ReplaceSubstring : IRenameOperation
        {

            /// <summary>Trieda IForm_MaskedRenameEditor</summary>
            protected class IForm_ReplaceSubstring : IForm
            {
                public IRenameOperation_ReplaceSubstring Value;
                public string ExampleFileName;

                protected IGroupBox FGrp;
                protected IButton FBtn_Cancel;
                protected IButton FBtn_OK;

                protected ILabel FLabel1;
                protected IComboBox FCombo_ApplyTo;
                protected ILabel FLabel2;
                protected ITextBox FText_ReplaceFrom;
                protected ILabel FLabel3;
                protected ITextBox FText_ReplaceTo;
                protected ILabel FLabel4;
                protected ITextBox FText_Preview;
                protected ICheckBox FCheck_IgnoreCase;

                /// <summary>Konstruktor</summary>
                public IForm_ReplaceSubstring()
                {
                    Value = new IRenameOperation_ReplaceSubstring();
                }

                /// <summary>Metoda: OnCreateForm</summary>
                /// <returns>true / false</returns>
                protected override bool OnCreateForm()
                {
                    if (!base.OnCreateForm()) return false; // Volam predchodcu

                    // Inicializacia
                    MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = true; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

                    SetBounds(0, 0, 400, 340); System.Drawing.Size CS = ClientSize;
                    Text = lang.FORM_MULTI_RENAME_TOOL_TEXT_12;

                    // Vytvorim groupbox
                    FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);
                    // Vytvorim tlacidla
                    FBtn_Cancel = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_2); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                    FBtn_OK = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_9, Event_BtnClickOK); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);
                    Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

                    FLabel1 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_1);
                    FCombo_ApplyTo = new IComboBox(); FCombo_ApplyTo.AddMultiItems(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_2, '\b', new int[] { IRenameOperation.APPLY_TO_FULL_NAME, IRenameOperation.APPLY_TO_NAME_ONLY, IRenameOperation.APPLY_TO_EXTENSION });
                    FLabel2 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_3);
                    FText_ReplaceFrom = new ITextBox();
                    FLabel3 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_4);
                    FText_ReplaceTo = new ITextBox();
                    FLabel4 = new ILabel(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_5);
                    FText_Preview = new ITextBox(); FText_Preview.ReadOnly = true;
                    FCheck_IgnoreCase = new ICheckBox(lang.FORM_MULTI_RENAME_TOOL_TEXT_12_6);

                    FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel1, FCombo_ApplyTo, FLabel2, FText_ReplaceFrom, FLabel3, FText_ReplaceTo, FCheck_IgnoreCase, FLabel4, FText_Preview });
                    IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel1, FGrp, 10, 18);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FCombo_ApplyTo, FLabel1, 0, 6, FGrp.ClientSize.Width - 20);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel2, FCombo_ApplyTo, 0, 10);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_ReplaceFrom, FLabel2, 0, 6, FGrp.ClientSize.Width - 20);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel3, FText_ReplaceFrom, 0, 10);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_ReplaceTo, FLabel3, 0, 6, FGrp.ClientSize.Width - 20);
                    IControl.PlaceControl_Outside_TopRight_RightBottom(FCheck_IgnoreCase, FText_ReplaceTo, 0, 10);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FLabel4, FText_ReplaceTo, 0, 20);
                    IControl.PlaceControl_Outside_LeftTop_LeftBottom(FText_Preview, FLabel4, 0, 6, FGrp.ClientSize.Width - 20);

                    CancelButton = FBtn_Cancel; AcceptButton = FBtn_OK;
                    FillForm(Value); ApplyChanges();

                    FText_ReplaceFrom.TextChanged += Event_OnChange;
                    FText_ReplaceTo.TextChanged += Event_OnChange;
                    FCombo_ApplyTo.SelectedIndexChanged += Event_OnChange;
                    FCheck_IgnoreCase.CheckedChanged += Event_OnChange;

                    return true; // Vsetko OK
                }

                /// <summary>Vyplnenie formulara</summary>
                /// <param name="pValue">objekt</param>
                protected void FillForm(IRenameOperation_ReplaceSubstring pValue)
                {
                    FCombo_ApplyTo.SelectedIntID = pValue.ApplyTo;
                    FText_ReplaceFrom.Text = pValue.ReplaceFrom;
                    FText_ReplaceTo.Text = pValue.ReplaceTo;
                    FCheck_IgnoreCase.Checked = pValue.IgnoreCase;
                }
                /// <summary>Nacitanie obsahu objektu</summary>
                /// <param name="Result">vysledok</param>
                protected void ReadForm(IRenameOperation_ReplaceSubstring Result)
                {
                    Result.ApplyTo = FCombo_ApplyTo.SelectedIntID;
                    Result.ReplaceFrom = FText_ReplaceFrom.Text;
                    Result.ReplaceTo = FText_ReplaceTo.Text;
                    Result.IgnoreCase = FCheck_IgnoreCase.Checked;
                }
                /// <summary>Aplikacia zmien</summary>
                protected void ApplyChanges()
                {
                    IRenameOperation_ReplaceSubstring V = new IRenameOperation_ReplaceSubstring();
                    ReadForm(V);
                    FText_Preview.Text = V.DoRename(ExampleFileName);
                }

                /// <summary>Event: Click pre tlacidlo OK</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_BtnClickOK(object pSender, EventArgs e)
                {
                    ReadForm(Value); // Nacitam formular
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                /// <summary>Event: OnChange</summary>
                /// <param name="pSender">zdroj</param>
                /// <param name="e">parameter</param>
                protected void Event_OnChange(object pSender, EventArgs e)
                {
                    ApplyChanges();
                }
            }
            
            public string ReplaceFrom; // Co sa ma nahradit
            public string ReplaceTo; // Cim sa ma nahradit
            public bool IgnoreCase; // Priznak ignorovania case

            /// <summary>Property: Name</summary>
            public override string Name
            {
                get
                {
                    switch (FApplyTo)
                    {
                        case APPLY_TO_NAME_ONLY: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_8, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_2);
                        case APPLY_TO_EXTENSION: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_8, lang.FORM_MULTI_RENAME_TOOL_TEXT_5_3);
                        default: return string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_5_8, "");
                    }
                }
            }

            /// <summary>Konstruktor</summary>
            public IRenameOperation_ReplaceSubstring(int pApplyTo = APPLY_TO_FULL_NAME)
                : base(pApplyTo)
            {
                ID = ID_REPLACE_SUBSTRING;
                IgnoreCase = false;
                ReplaceFrom = ""; ReplaceTo = "";
            }

            /// <summary>Vykonanie premenovania</summary>
            /// <param name="pValue">vstup</param>
            /// <returns>vystup</returns>
            public override string DoRename(string pValue, long pCounter = 1)
            {
                string N, E;
                SplitName(pValue, out N, out E); // Rozdelim

                switch (FApplyTo)
                {
                    case APPLY_TO_EXTENSION: if (ReplaceFrom != "") E = ICore.ReplaceStr(E, ReplaceFrom, ReplaceTo, IgnoreCase); break;
                    case APPLY_TO_NAME_ONLY: if (ReplaceFrom != "") N = ICore.ReplaceStr(N, ReplaceFrom, ReplaceTo, IgnoreCase); break;
                    default:
                        {
                            if (ReplaceFrom != "") N = ICore.ReplaceStr(N, ReplaceFrom, ReplaceTo, IgnoreCase);
                            if (ReplaceFrom != "") E = ICore.ReplaceStr(E, ReplaceFrom, ReplaceTo, IgnoreCase);
                        } break;
                }

                return JoinName(N, E); // Spojim a vratim vysledok
            }
            /// <summary>Skopirovanie obsahu objektu</summary>
            /// <param name="pValue">objekt pre skopirovanie</param>
            public void Assign(IRenameOperation_ReplaceSubstring pValue)
            {
                FApplyTo = pValue.FApplyTo;
                ReplaceFrom = pValue.ReplaceFrom;
                ReplaceTo = pValue.ReplaceTo;
                IgnoreCase = pValue.IgnoreCase;
            }
            /// <summary>Zmena parametrov pomocou formulara</summary>
            /// <returns></returns>
            public bool EditWithForm(string pTestValue)
            {
                using (IForm_ReplaceSubstring F = new IForm_ReplaceSubstring())
                {
                    F.Value.Assign(this); F.ExampleFileName = pTestValue;
                    bool B = F.ShowDialog() == System.Windows.Forms.DialogResult.OK;
                    if (B) Assign(F.Value);
                    return B;
                }
            }
        }

        public IMultiRenameData_Str Value; // Objekt pre editaciu
        protected System.Collections.Generic.List<IRenameOperation> FOperations; // Zoznam operacii
        protected System.Collections.Generic.List<string> FData_Col0;
        protected System.Collections.Generic.List<string> FData_Col1;
        protected System.Collections.Generic.List<string> FData_Col2;

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_Cancel;
        protected IButton FBtn_OK;
        protected IButton FBnt_Operations;

        protected IListView FList_Operations;
        protected IListView FList_Items;

        /// <summary>Property: CurrentOriginalName</summary>
        protected string CurrentOriginalName
        {
            get
            {
                if (FList_Items.ItemsCount == 0) return "";
                int I = FList_Items.SelectedIndex;
                if (I == -1) I = 0;
                return FData_Col0[I];
            }
        }

        /// <summary>Konstruktor</summary>
        public IFileExplorerForm_MultiRenameTool()
        {
            Value = new IMultiRenameData_Str();
            FOperations = new System.Collections.Generic.List<IRenameOperation>();
            FData_Col0 = new System.Collections.Generic.List<string>();
            FData_Col1 = new System.Collections.Generic.List<string>();
            FData_Col2 = new System.Collections.Generic.List<string>();
        }

        /// <summary>Metoda: OnCreateForm</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            // Inicializacia
            MinimizeBox = false; MaximizeBox = false; FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; ShowIcon = true; ShowInTaskbar = true; StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

            SetBounds(0, 0, 700, 540); System.Drawing.Size CS = ClientSize;
            Text = lang.FORM_MULTI_RENAME_TOOL_TEXT_1;

            // Vytvorim groupbox
            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50);
            // Vytvorim tlacidla
            FBtn_Cancel = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_2); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_9, Event_BtnClick_OK); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);
            FBnt_Operations = new IButton(lang.FORM_MULTI_RENAME_TOOL_TEXT_13, Event_BtnClick_Operations); IControl.PlaceControl_Outside_LeftTop_LeftBottom(FBnt_Operations, FGrp, 0, 10);
            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBnt_Operations, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            FList_Operations = new IListView(); FList_Operations.MultiSelect = false; FList_Operations.FullRowSelect = true; FList_Operations.HideSelection = false;
            FList_Items = new IListView(); FList_Items.MultiSelect = false; FList_Items.FullRowSelect = true; FList_Items.VirtualMode = true; FList_Items.HideSelection = false;

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FList_Operations, FList_Items });
            IControl.PlaceControl_Inside_LeftTop_LeftTop(FList_Operations, FGrp, 10, 10, FGrp.ClientSize.Width - 20, 200);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FList_Items, FList_Operations, 0, 10, FGrp.ClientSize.Width - 20, FGrp.ClientSize.Height - FList_Operations.Bottom - 20);

            FList_Operations.CreateColumn(lang.FORM_MULTI_RENAME_TOOL_TEXT_3, FList_Operations.ClientSize.Width - 10, System.Windows.Forms.HorizontalAlignment.Left);
            FList_Items.CreateColumn(lang.FORM_MULTI_RENAME_TOOL_TEXT_4_1, 200, System.Windows.Forms.HorizontalAlignment.Left);
            FList_Items.CreateColumn(lang.FORM_MULTI_RENAME_TOOL_TEXT_4_2, 200, System.Windows.Forms.HorizontalAlignment.Left);
            FList_Items.CreateColumn(lang.FORM_MULTI_RENAME_TOOL_TEXT_4_3, 400, System.Windows.Forms.HorizontalAlignment.Left);

            FList_Operations.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            CreatePopupMenu(FList_Operations.ContextMenuStrip.Items);

            CancelButton = FBtn_Cancel; AcceptButton = FBtn_OK;

            // Eventy
            FList_Operations.ItemActivate += Event_MIClick_EditOperation;
            FList_Items.RetrieveVirtualItem += Event_RetrieveVirtualItem;

            CreateItemsList();
            FillOperationsList();
            return true; // Vsetko OK
        }

        /// <summary>Vytvorenie zoznamu poloziek</summary>
        protected void CreateItemsList()
        {
            FData_Col0.Clear(); FData_Col1.Clear(); FData_Col2.Clear(); // Vymazem obsah

            int C = Value.SourceFileNames.Count;
            for (int i = 0; i < C; i++)
            {
                string FN = Value.SourceFileNames[i];
                string FN1 = IFileSystem.ExtractName(FN);
                string FN2 = IFileSystem.ExtractPath(FN);
                FData_Col0.Add(FN1); FData_Col1.Add(FN1); FData_Col2.Add(FN2);
            }

            FList_Items.BeginCreate();
            FList_Items.VirtualListSize = Value.SourceFileNames.Count;
            FList_Items.EndCreate(-1);
        }
        /// <summary>Vytvorenie popup menu</summary>
        /// <param name="pParent">rodicovska kolekcia</param>
        protected void CreatePopupMenu(System.Windows.Forms.ToolStripItemCollection pParent)
        {
            IToolStripMenuItem M1;

            pParent.Clear();
            M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1);
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_5, Event_MIClick_NewOperation).Tag = 5;
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_6, Event_MIClick_NewOperation).Tag = 6;
            M1.CreateSeparator();
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_1, Event_MIClick_NewOperation).Tag = 1;
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_2, Event_MIClick_NewOperation).Tag = 2;
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_3, Event_MIClick_NewOperation).Tag = 3;
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_1_4, Event_MIClick_NewOperation).Tag = 4;

            IToolStrip.CreateSeparator(pParent);
            IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_6_5, Event_MIClick_EditOperation);
            IToolStrip.CreateSeparator(pParent);

            M1 = IToolStrip.CreateMenuItem(pParent, lang.FORM_MULTI_RENAME_TOOL_TEXT_6_2);
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_3, Event_MIClick_DeleteOperation);
            M1.CreateSeparator();
            M1.CreateMenuItem(lang.FORM_MULTI_RENAME_TOOL_TEXT_6_4, Event_MIClick_ClearOperations);
        }
        /// <summary>Vyplnenie zoznamu operacii</summary>
        protected void FillOperationsList()
        {
            int Idx = FList_Operations.SelectedIndex;

            FList_Operations.BeginCreate();
            FList_Operations.Items.Clear();

            int I = 0;
            foreach (IRenameOperation O in FOperations) // Vsetky operacie
            {
                FList_Operations.AddItem(null, O.Name, I++);
            }

            FList_Operations.EndCreate(Idx);
        }
        /// <summary>Vyvola sa okno na urcenie rozsahu aplikacie operatora</summary>
        /// <returns>-1 pri zruseni alebo ID rozsahu</returns>
        protected int ChooseTarget()
        {
            int R = IRenameOperation.APPLY_TO_FULL_NAME;
            return IApp.ShowEditor_ChooseFromList(lang.FORM_MULTI_RENAME_TOOL_TEXT_1, lang.FORM_MULTI_RENAME_TOOL_TEXT_7_1, lang.FORM_MULTI_RENAME_TOOL_TEXT_7_2, lang.FORM_MULTI_RENAME_TOOL_TEXT_7_3, ref R) ? R : -1;
        }
        /// <summary>Aplikovanie zadanych operacii na zoznam</summary>
        protected void ApplyOperations()
        {
            IApp.App.WaitCursor = true;

            foreach (IRenameOperation O in FOperations) O.InitCounter();
            
            int C = FData_Col0.Count;

            for (int i = 0; i < C; i++)
            {
                string FN = FData_Col0[i];
                foreach (IRenameOperation O in FOperations)
                {
                    FN = O.DoRename(FN, O.Counter);
                    O.IncreaseCounter();
                }
                FData_Col1[i] = FN;
            }
            FList_Items.Invalidate();
            IApp.App.WaitCursor = false;
        }
        /// <summary>Vytvorenie vysledku</summary>
        /// <returns>true / false</returns>
        protected int CreateResult()
        {
            IApp.App.WaitCursor = true;
            foreach (IRenameOperation O in FOperations) O.InitCounter();
            System.Collections.Generic.List<string> SourceList = new System.Collections.Generic.List<string>();
            System.Collections.Generic.List<string> DestList = new System.Collections.Generic.List<string>();

            int C = FData_Col0.Count;
            bool Changed = false;

            for (int i = 0; i < C; i++)
            {
                string FN = FData_Col0[i]; // Zdroj
                SourceList.Add(IFileSystem.AppendToPath(FData_Col2[i], FN)); // Zaradim do zdroja

                string FN1 = FN;
                foreach (IRenameOperation O in FOperations)
                {
                    FN = O.DoRename(FN, O.Counter);
                    O.IncreaseCounter();
                }
                if (!IFileSystem.IsValidFileName(FN))
                {
                    IApp.ShowError(string.Format(lang.FORM_MULTI_RENAME_TOOL_TEXT_11, FN));
                    FList_Items.SelectedIndex = i;
                    IApp.App.WaitCursor = false;
                    return -1;
                }
                DestList.Add(FN); // Zaradim do ciela
                Changed |= FN != FN1; // Zaznamena priznak zmeny
            }

            IApp.App.WaitCursor = false;
            if (!Changed) return 0; // Ziadna zmena?

            Value.SourceFileNames.Clear(); Value.SourceFileNames.AddRange(SourceList); // Nakopirujem do vysledku
            Value.DestinationFileNames.Clear(); Value.DestinationFileNames.AddRange(DestList);

            return 1; // Je zmena
        }

        /// <summary>Event: OnClick pre polozku menu 'Nova operacia'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MIClick_NewOperation(object pSender, EventArgs e)
        {
            int ID = ICore.ToInt(((IToolStripMenuItem)pSender).Tag);

            switch (ID)
            {
                case 1:
                    {
                        int T = ChooseTarget();
                        if (T != -1)
                        {
                            FOperations.Add(new IRenameOperation_UpperCase(T));
                            FillOperationsList();
                        }
                    } break;
                case 2:
                    {
                        int T = ChooseTarget();
                        if (T != -1)
                        {
                            FOperations.Add(new IRenameOperation_LowerCase(T));
                            FillOperationsList();
                        }
                    } break;
                case 3:
                    {
                        int T = ChooseTarget();
                        if (T != -1)
                        {
                            FOperations.Add(new IRenameOperation_FirstUpperCase(T));
                            FillOperationsList();
                        }
                    } break;
                case 4:
                    {
                        int T = ChooseTarget();
                        if (T != -1)
                        {
                            FOperations.Add(new IRenameOperation_FirstWordUpperCase(T));
                            FillOperationsList();
                        }
                    } break;
                case 5:
                    {
                        IRenameOperation_MaskedRename P = new IRenameOperation_MaskedRename();
                        if (!P.EditWithForm(CurrentOriginalName)) return;
                        FOperations.Add(P);
                        FillOperationsList();
                    } break;
                case 6:
                    {
                        IRenameOperation_ReplaceSubstring P = new IRenameOperation_ReplaceSubstring();
                        if (!P.EditWithForm(CurrentOriginalName)) return;
                        FOperations.Add(P);
                        FillOperationsList();
                    } break;
            }
            ApplyOperations();
        }
        /// <summary>Event: OnClick pre polozku menu 'Zmena operacie'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MIClick_EditOperation(object pSender, EventArgs e)
        {
            int Idx = FList_Operations.SelectedIntTag;
            if (Idx == -1) return;

            switch (FOperations[Idx].ID) // Podla typu operacie
            {
                case IRenameOperation.ID_REPLACE_SUBSTRING:
                    {
                        if (!((IRenameOperation_ReplaceSubstring)FOperations[Idx]).EditWithForm(CurrentOriginalName)) return;
                    } break;
                case IRenameOperation.ID_MASKED_RENAME:
                    {
                        if (!((IRenameOperation_MaskedRename)FOperations[Idx]).EditWithForm(CurrentOriginalName)) return;
                    } break;
                case IRenameOperation.ID_UPPER_CASE:
                case IRenameOperation.ID_LOWER_CASE:
                case IRenameOperation.ID_FIRST_UPPER_CASE:
                case IRenameOperation.ID_FIRST_WORD_UPPER_CASE:
                    {
                        int T = ChooseTarget();
                        if (T != -1)
                        {
                            FOperations[Idx].ApplyTo = T;
                            FillOperationsList();
                        }
                    } break;
            }
            ApplyOperations();
        }
        /// <summary>Event: OnClick pre polozku menu 'Vymazat vsetky operacie'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MIClick_ClearOperations(object pSender, EventArgs e)
        {
            if (IApp.ShowQuestion(lang.FORM_MULTI_RENAME_TOOL_TEXT_8))
            {
                FOperations.Clear();
                FillOperationsList();
                ApplyOperations();
            }
        }
        /// <summary>Event: OnClick pre polozku menu 'Vymazat vsetky operacie'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_MIClick_DeleteOperation(object pSender, EventArgs e)
        {
            int Idx = FList_Operations.SelectedIndex;
            if (Idx == -1) return; // Ziadna vybrana polozka?

            if (IApp.ShowQuestion(lang.FORM_MULTI_RENAME_TOOL_TEXT_8_1))
            {
                FOperations.RemoveAt(Idx);
                FillOperationsList();
                ApplyOperations();
            }
        }
        /// <summary>Event: OnClick pre tlacidlo OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnClick_OK(object pSender, EventArgs e)
        {
            int R = CreateResult();
            if (R < 0) return; // Chyba?
            DialogResult = R == 1 ? System.Windows.Forms.DialogResult.OK : System.Windows.Forms.DialogResult.Cancel;
        }
        /// <summary>Event: RetrieveVirtualItem</summary>
        /// <param name="sender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_RetrieveVirtualItem(object sender, System.Windows.Forms.RetrieveVirtualItemEventArgs e)
        {
            if (e.Item == null)
            {
                e.Item = new System.Windows.Forms.ListViewItem();
                e.Item.SubItems.Add(""); e.Item.SubItems.Add("");
            }
            e.Item.SubItems[0].Text = FData_Col0[e.ItemIndex];
            e.Item.SubItems[1].Text = FData_Col1[e.ItemIndex];
            e.Item.SubItems[2].Text = FData_Col2[e.ItemIndex];
        }
        /// <summary>Event: OnClick pre tlacidlo 'Operacie'</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnClick_Operations(object pSender, EventArgs e)
        {
            FList_Operations.ContextMenuStrip.Show(MousePosition);
        }

    }
}
